import time
import random
from Sentry3 import *
from mpython import *
from machine import UART


uart1 = UART(1, baudrate=57600, tx=Pin.P14, rx=Pin.P13)

Sentry3 = Sentry3(0x60)

random.seed(time.ticks_cpu())

# 自定义函数
def UART():
  global my_variable, key_N, key_P, key_T, key_H, key_A, key_B, param_set, i, I2C, index, num
  Sentry3.begin(uart1)
def I2C():
  global my_variable, key_N, key_P, key_T, key_H, key_A, key_B, param_set, i, I2C, index, num
  # Sentry3默认I2C，通讯地址唯一：0x60。主控器通过I2C总线连接多个设备时，请注意要避免出现地址冲突
  Sentry3.begin(i2c)
def Param_Set():
  global my_variable, key_N, key_P, key_T, key_H, key_A, key_B, param_set, i, I2C, index, num
  if (param_set == 1):
    oled.fill_rect(0, (2-1)*16, 128, 16, 0)
    oled.fill_rect(0, (3-1)*16, 128, 16, 0)
    oled.fill_rect(0, (4-1)*16, 128, 16, 0)
    oled.show()
    Key_A()
    Key_B()
    oled.DispChar("Operation OK!", 0, (2-1)*16, 1)
    oled.show()
    time.sleep(0.2)
    oled.fill_rect(0, (2-1)*16, 128, 16, 0)
    oled.show()
    param_set = 0
def Key_A():
  global my_variable, key_N, key_P, key_T, key_H, key_A, key_B, param_set, i, I2C, index, num
  if (key_A == 1):
    key_A = 0
    index = (random.randint(1, 6))
    oled.DispChar((str(index)), 112, 0, 1)
    oled.show()
    Sentry3.SetParamNum(sentry3_vision_e.kVisionBlob,index)
    Sentry3.VisionSetMode(sentry3_vision_e.kVisionBlob,1)
    i = 1
    for index in range(index):
      if (i == 1):
        Sentry3.SetParam(sentry3_vision_e.kVisionBlob,[0, 0, 8, 8, color_label_e.kColorBlack],1)
      if (i == 2):
        Sentry3.SetParam(sentry3_vision_e.kVisionBlob,[0, 0, 8, 8, color_label_e.kColorWhite],2)
      if (i == 3):
        Sentry3.SetParam(sentry3_vision_e.kVisionBlob,[0, 0, 8, 8, color_label_e.kColorRed],3)
      if (i == 4):
        Sentry3.SetParam(sentry3_vision_e.kVisionBlob,[0, 0, 8, 8, color_label_e.kColorGreen],4)
      if (i == 5):
        Sentry3.SetParam(sentry3_vision_e.kVisionBlob,[0, 0, 8, 8, color_label_e.kColorBlue],5)
      if (i == 6):
        Sentry3.SetParam(sentry3_vision_e.kVisionBlob,[0, 0, 8, 8, color_label_e.kColorYellow],6)
      # 等待参数生效
      time.sleep(0.1)
      i = (i + 1)
def Key_B():
  global my_variable, key_N, key_P, key_T, key_H, key_A, key_B, param_set, i, I2C, index, num
  if (key_B == 1):
    key_B = 0
    index = (random.randint(1, 6))
    oled.DispChar((str(index)), 112, 0, 1)
    oled.show()
    Sentry3.SetParamNum(sentry3_vision_e.kVisionBlob,1)
    Sentry3.VisionSetMode(sentry3_vision_e.kVisionBlob,5)
    if (index == 1):
      Sentry3.SetParam(sentry3_vision_e.kVisionBlob,[0, 0, 8, 8, color_label_e.kColorBlack],1)
    if (index == 2):
      Sentry3.SetParam(sentry3_vision_e.kVisionBlob,[0, 0, 8, 8, color_label_e.kColorWhite],1)
    if (index == 3):
      Sentry3.SetParam(sentry3_vision_e.kVisionBlob,[0, 0, 8, 8, color_label_e.kColorRed],1)
    if (index == 4):
      Sentry3.SetParam(sentry3_vision_e.kVisionBlob,[0, 0, 8, 8, color_label_e.kColorGreen],1)
    if (index == 5):
      Sentry3.SetParam(sentry3_vision_e.kVisionBlob,[0, 0, 8, 8, color_label_e.kColorBlue],1)
    if (index == 6):
      Sentry3.SetParam(sentry3_vision_e.kVisionBlob,[0, 0, 8, 8, color_label_e.kColorYellow],1)
    # 等待参数生效
    time.sleep(0.1)
def Result_Dispaly():
  global my_variable, key_N, key_P, key_T, key_H, key_A, key_B, param_set, i, I2C, index, num
  if (param_set == 0):
    # Sentry3不主动返回检测识别结果，需要主控板发送指令进行读取。读取的流程：首先读取识别结果的数量，接收到指令后，Sentry3会刷新结果数据，如果结果数量不为零，那么主控再发送指令读取结果的相关信息。请务必按此流程构建程序。
    num = (Sentry3.GetValue(sentry3_vision_e.kVisionBlob, sentry_obj_info_e.kStatus))
    i = 1
    for index in range(num):
      if (Sentry3.GetValue(sentry3_vision_e.kVisionBlob,sentry_obj_info_e.kLabel,i)==color_label_e.kColorBlack):
        oled.DispChar((str("#") + str((str((str(i))) + str(": Black")))), 0, (2-1)*16, 1)
      if (Sentry3.GetValue(sentry3_vision_e.kVisionBlob,sentry_obj_info_e.kLabel,i)==color_label_e.kColorWhite):
        oled.DispChar((str("#") + str((str((str(i))) + str(": White")))), 0, (2-1)*16, 1)
      if (Sentry3.GetValue(sentry3_vision_e.kVisionBlob,sentry_obj_info_e.kLabel,i)==color_label_e.kColorRed):
        oled.DispChar((str("#") + str((str((str(i))) + str(": Red")))), 0, (2-1)*16, 1)
      if (Sentry3.GetValue(sentry3_vision_e.kVisionBlob,sentry_obj_info_e.kLabel,i)==color_label_e.kColorGreen):
        oled.DispChar((str("#") + str((str((str(i))) + str(": Green")))), 0, (2-1)*16, 1)
      if (Sentry3.GetValue(sentry3_vision_e.kVisionBlob,sentry_obj_info_e.kLabel,i)==color_label_e.kColorBlue):
        oled.DispChar((str("#") + str((str((str(i))) + str(": Blue")))), 0, (2-1)*16, 1)
      if (Sentry3.GetValue(sentry3_vision_e.kVisionBlob,sentry_obj_info_e.kLabel,i)==color_label_e.kColorYellow):
        oled.DispChar((str("#") + str((str((str(i))) + str(": Yellow")))), 0, (2-1)*16, 1)
      oled.DispChar((str("x=") + str((Sentry3.GetValue(sentry3_vision_e.kVisionBlob,sentry_obj_info_e.kXValue,i)))), 0, 32, 1)
      oled.DispChar((str("y=") + str((Sentry3.GetValue(sentry3_vision_e.kVisionBlob,sentry_obj_info_e.kYValue,i)))), 64, 32, 1)
      oled.DispChar((str("w=") + str((Sentry3.GetValue(sentry3_vision_e.kVisionBlob,sentry_obj_info_e.kWidthValue,i)))), 0, 48, 1)
      oled.DispChar((str("h=") + str((Sentry3.GetValue(sentry3_vision_e.kVisionBlob,sentry_obj_info_e.kHeightValue,i)))), 64, 48, 1)
      oled.show()
      time.sleep(0.1)
      oled.fill_rect(0, (2-1)*16, 128, 16, 0)
      oled.fill_rect(0, (3-1)*16, 128, 16, 0)
      oled.fill_rect(0, (4-1)*16, 128, 16, 0)
      oled.show()
      i = (i + 1)

# 事件回调函数
def on_button_a_down(_):
  time.sleep_ms(10)
  if button_a.value() == 1: return
  global param_set
  global key_A
  param_set = 1
  key_A = 1
def on_button_b_down(_):
  time.sleep_ms(10)
  if button_b.value() == 1: return
  global param_set
  global key_B
  param_set = 1
  key_B = 1


button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)
param_set = 0
key_A = 0
key_B = 0
index = 0
num = 0
i = 0
# 等待Sentry3完成操作系统的初始化。此等待时间不可去掉，避免出现Sentry3尚未初始化完毕主控器已经开发发送指令的情况
time.sleep(2)
I2C()
Sentry3.VisionBegin(sentry3_vision_e.kVisionBlob)
# 算法的默认参数，初始化时可以省略
Sentry3.SetParamNum(sentry3_vision_e.kVisionBlob,1)
# 算法的默认参数，初始化时可以省略
Sentry3.SetParam(sentry3_vision_e.kVisionBlob,[0, 0, 3, 4, color_label_e.kColorRed],1)
# 算法的默认参数，初始化时可以省略
Sentry3.VisionSetMode(sentry3_vision_e.kVisionBlob,1)
oled.DispChar("Algo: Blob", 0, 0, 1)
oled.DispChar("index: ", 64, 0, 1)
oled.DispChar((str(index)), 112, 0, 1)
oled.show()
while True:
  Param_Set()
  Result_Dispaly()
